/**
 * Created by kosmoknot on 8/19/2014.
 */

/**
 * Created by kosmoknot on 8/19/2014.
 */

function onCloudLabelMouseEvent ( event, cloudData, stage, canvas )
{
    var index = 0;
    var count = cloudData[index].count;
    var item;
    var total = 0;
    for ( i = 0; i< cloudData.length; i++) {
        item = cloudData[i];
        if ( item.count != null )
        {
            total += parseFloat(item.count);
        }
    }
    var itemWidth;
    var barX=0;

    for ( i = 0; i< cloudData.length; i++) {
        item = cloudData[i];
        var count = 0;
        if (item.count != null) {
            count = parseFloat(item.count);
        }

        itemWidth = canvas.width * count / total;
        itemHeight = canvas.width;

        if ( event.stageX >= barX && event.stageX <= barX + itemWidth )
        {
            break;
        } else {
            barX += itemWidth;
        }

    }

    if ( event.type == "click")
    {
        if ( item != null )
        {
            window.location.href = "#/topics/" + item.uuid + "&&" + "" + "&&" + "";
            return;
        }

    }

    return;

    if ( event.type == "mouseover")
    {
        if ( stage.children.length > 1 )
        {
            stage.removeChildAt ( 1 );
            stage.update();
        }

        var container = new createjs.Container();

        var label = new createjs.Text(count, "20px Arial", "#333333");
        label.text = count;
        label.textAlign = "center";
        container.addChild(label);

        var lw = label.getMeasuredWidth();
        var lh = label.getMeasuredHeight();
        label.x = 0; //( itemWidth - lw )/2;
        label.y = 0; ( itemHeight - lh )/2;

        var rectangle = new createjs.Shape();
        rectangle.graphics.beginStroke("#333333").beginFill("#FFFFFF").drawRect( 0,0,lw+10,lh+10 );
        rectangle.graphics.endFill();
        container.addChildAt(rectangle,0);

        container.x = barX + ( itemWidth - rectangle.width )/2;
        container.y = ( itemHeight- rectangle.height )/2;

        stage.addChild( container );
        stage.update();
    }

    if ( event.type == "mouseout")
    {
        if ( stage.children.length > 1 )
        {
            stage.removeChildAt ( 1 );
            stage.update();
        }
    }
}

function createBarCloud (  canvas, data, barColors  ) {

    if ( barColors == null )
    {
        barColors = ["#69d2e7", "#a7dbd8", "#e0e4cc", "#f38630", "#fa6900", "#490A3D", "#BD1550", "#e97f02", "#F8ca00", "#8a9b0f", "#D9CEB2", "#424242"];
    }

    if ( data == null )
    {
        data = [{ "uuid": "4521191813414912", "name": "Arts and Literature", "count": "30"}, { "uuid": "4661929301770240", "name": "Society", "count": "10"}, { "uuid": "4802666790125568", "name": "Life and the Biosphere", "count": "10"}, { "uuid": "5084141766836224", "name": "Health and Medicine", "count": "0"}, { "uuid": "5224879255191552", "name": "Technology", "count": "0"}, { "uuid": "5365616743546880", "name": "Plants", "count": "12"}, { "uuid": "5647091720257536", "name": "Earth and Geography", "count": "26"}, { "uuid": "5787829208612864", "name": "Sports and Recreation", "count": "1"}, { "uuid": "5928566696968192", "name": "Philosophy and Religion", "count": "19"}, { "uuid": "6210041673678848", "name": "History", "count": "23"}, { "uuid": "6491516650389504", "name": "Science and Mathematics", "count": "0"}, { "uuid": "6720215068966912", "name": "Animals", "count": "1"}];
    }

    cloudData = data;

    var stage = new createjs.Stage(canvas);
    stage.enableMouseOver();
    var cloudContainer = new createjs.Container();

    var total = 0;
    for ( i = 0; i< data.length; i++) {
        item = data[i];
        if ( item.count == null || item.count == "0" )
        {
            item.count = "1";
        }
        if ( item.count != null )
        {
            total += parseFloat(item.count);
        }
    }

    var barX=0;
    for ( i = 0; i< data.length; i++) {
        item = data[i];
        var count = 0;
        if ( item.count != null )
        {
            count = parseFloat(item.count);
        }
        var itemContainer = new createjs.Container();
        itemContainer.x = barX;
        var itemWidth = canvas.width*count/total;
        var itemHeight = canvas.height;
        var rectangle = new createjs.Shape();
        rectangle.graphics.beginStroke(barColors[i]).beginFill(barColors[i]).drawRect( 0,0,itemWidth, itemHeight );
        barX += itemWidth;
        rectangle.graphics.endFill();
        itemContainer.on("click", function (event)
        {
            onCloudLabelMouseEvent ( event, data, stage, canvas );
        });
        itemContainer.on("mouseover", function (event)
        {
            onCloudLabelMouseEvent ( event, data, stage, canvas );
        });
        itemContainer.on("mouseout", function (event)
        {
            onCloudLabelMouseEvent ( event, data, stage, canvas );
        });
        itemContainer.addChild(rectangle);
        cloudContainer.addChild ( itemContainer );
    }

    stage.addChild ( cloudContainer );

    stage.update();

}